<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use \App\Home\Course;
use \App\Home\Profession;
use Input;
use Auth;

class CourseController extends Controller
{
    public function checkLogin(){
        $user = Auth::guard('home')->user();
        if(empty($user) == true){
            $data = array(
                'errorMessage' => '尚未登录！',
                'success'      => false,
            );
            return response() -> json($data);
        }
    }

    //获取培训课程的分类
    public function getDirections(){
        $data =  Course::select('id','course_name') -> orderBy('sort','desc') -> where('type','1') -> get();
        return response() -> json($data);
    }

    //视频播放详情页
    public function videoPlay(){
        return view('home.lession.videoplay');
    }



    //目录
    public function getVideoTree(){
        $this -> checkLogin();

        $courseId = Input::get('courseId');
        if(empty($courseId) == true){
            $data['status'] = 0;
            return response() -> json($data);
        }
        $res = Course::select('id','course_name') -> with('rel_lessions') -> whereIdAndStatus($courseId,'2') -> first();
        if(empty($res) == true){
            $data['status'] = 0;
        }else{
            $data = array(
                'status' => 200,
                'result' => $res
            );
        }
        return response() -> json($data);
    }

    //演员/讲师信息
    public function getLecturers(){
        $this -> checkLogin();

    }

    //课程介绍详情页
    public function courseIntroduction(){
        return view('home.lession.index');
    }
    //课程介绍详情页
    public function getCourseIntroduction($id){
        $res = Course::where('id',$id) -> increment('click_num');
        $data = Course::select('id','course_name','free','price','cover_img','course_detail','recommend_type','description') -> with('rel_lessions') -> whereIdAndStatus($id,'2') -> first();
        $data['del_price'] = $data['price'] + 1599;
        $data['courseLength'] = 0;
        foreach($data['rel_lessions'] as $key => $value){
            $data['courseLength'] += $value['video_time'];
        }
        return response() -> json($data);
    }

    //直播详情页
    public function livePlay($id){
        return view('home.lession.index');
    }

    //详情页中获取推荐课程
    public function getRecommendCourse(){

    }

    //更新轮播图点击量
    public function updateClickCount(){
        $id = Input::get('id');
        $res = Course::where('id',$id) -> increment('click_num');
    }

    //片库
    public function index(){
        return view('home.course.index');
    }

    public function getProfessionCourses(){
        $protype_id = Input::get('pid');
        $pageNumber = Input::get('p') ? Input::get('p') : 1;
        $pageSize = 10;
        $offset = ($pageNumber - 1) * $pageSize;
        $list = Course::select('course.id','course.course_name','course.banner_img','course.update_number','course.course_detail','course.type','course.click_num','course.comment_num','course.fabulous_num','course.search_num') -> leftJoin('profession','profession_id','=','profession.id') -> where('profession.protype_id',$protype_id) -> offset($offset)
            -> limit($pageSize) -> orderBy('click_num','desc') -> get() -> toArray();
        $totalCount =Course::leftJoin('profession','profession_id','=','profession.id') -> where('profession.protype_id',$protype_id)  -> count();
        if($list){
            $totalPageCount = ceil($totalCount/$pageSize);
            $page = array(
                'pageSize'       => $pageSize,         //每页显示条数
                'totalPageCount' => $totalPageCount,   //总页数
                'currentPage'    => $pageNumber,       //当前页
                'totalCount'     => $totalCount,       //总条数
            );
            $data = array(
                'success' => true,
                'resultObject' => $list,
                'page' => $page
            );
        }else{
            $data['success'] = false;
        }
        return response() -> json($data);
    }
}